# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\aws\s3.py
__author__ = "Attila Gerendi (Sanyi)"
import logging, boto
from boto.s3.key import Key

_logger = logging.getLogger("helpers.aws.s3")


def upload_text(bucket_name, s3_file_name, data, connection=None):
    if connection is None:
        connection = boto.connect_s3()
    bucket = connection.get_bucket(bucket_name)
    k = Key(bucket)
    k.key = s3_file_name
    k.set_contents_from_string(data)


def upload_file(
    bucket_name,
    s3_file_name,
    local_file_name,
    content_type=None,
    content_disposition=None,
    connection=None,
    extra_headers=None,
):
    if connection is None:
        connection = boto.connect_s3()
    bucket = connection.get_bucket(bucket_name)
    k = Key(bucket)
    k.key = s3_file_name
    headers = {}
    if content_type is not None:
        headers["Content-Type"] = content_type
    if content_disposition is not None:
        headers["Content-Disposition"] = content_disposition
    if extra_headers:
        headers.update(extra_headers)
    k.set_contents_from_filename(local_file_name, headers=headers)


def download_file(bucket_name, s3_file_name, local_file_name, connection=None):
    if connection is None:
        connection = boto.connect_s3()
    bucket = connection.get_bucket(bucket_name)
    k = Key(bucket)
    k.key = s3_file_name
    k.get_contents_to_filename(local_file_name)


def file_exist(bucket_name, s3_file_name, connection=None):
    if connection is None:
        connection = boto.connect_s3()
    bucket = connection.get_bucket(bucket_name)
    k = Key(bucket)
    k.key = s3_file_name
    return k.exists()


def download_data(bucket_name, s3_file_name, connection=None):
    if connection is None:
        connection = boto.connect_s3()
    bucket = connection.get_bucket(bucket_name)
    k = Key(bucket)
    k.key = s3_file_name
    return k.get_contents_as_string()


def generate_link(bucket_name, file_name, expires, connection=None):
    if connection is None:
        connection = boto.connect_s3()
    return connection.generate_url(
        expires_in=expires,
        method="GET",
        bucket=bucket_name,
        key=file_name,
        headers=None,
        query_auth=True,
        force_http=False,
        response_headers=None,
        expires_in_absolute=False,
        version_id=None,
    )


def delete_file(bucket_name, s3_file_name, connection=None):
    if connection is None:
        connection = boto.connect_s3()
    bucket = connection.get_bucket(bucket_name)
    k = Key(bucket)
    k.key = s3_file_name
    k.delete()


def delete_files_by_prefix(bucket_name, s3_prefix, connection=None):
    if connection is None:
        connection = boto.connect_s3()
    bucket = connection.get_bucket(bucket_name)
    lst = bucket.list(prefix=s3_prefix)
    bucket.delete_keys([key.name for key in lst])
